﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using Kitware.VTK;


namespace HAVTKPack

{
   
    public class ShowGeometryPolyDataFilter:GeometryFilter
    {

        #region  构造函数

        public ShowGeometryPolyDataFilter(string id):base(id)
        { 
        
        
        }
        //public ShowGeometryPolyDataFilter(vtkUnstructuredGrid sourceGrid)
        //{
        //    this._unStructuredGridData = sourceGrid;

        //}

        #endregion


        #region 字段属性




      

        #endregion



        #region  VTK流水线所需要的对象

        vtkUnstructuredGrid _unStructuredGridData;

        vtkConnectivityFilter _con;

        vtkGeometryFilter _geo;

        vtkPolyData _polyData;


        vtkPolyDataMapper _polyDataMapper;

        vtkDataSetMapper setMapper;

        vtkActor _actor ;


        vtkLookupTable lut;


        //vtkCellDataToPointData cTop;


        #endregion


        public override void Render()
        {
            try
            {
                //if (this._unStructuredGridData ==null )
                //{
                //    return;
                //}


                _unStructuredGridData = (vtkUnstructuredGrid)this.ParentNode.Geometry.OutPutData;

                //_unStructuredGridData = this.ParentNode.Geometry.UnStructuredGridData;

                if (this._unStructuredGridData == null)
                {
                    return;
                }

                _con = vtkConnectivityFilter.New();
                _con.SetInput(_unStructuredGridData);
                _geo = vtkGeometryFilter.New();
                _geo.SetInput(_con.GetOutput());
                _polyData = vtkPolyData.New();
                _polyData = _geo.GetOutput();

                this.OutputPolyData = _geo.GetOutput();
                this.OutputUnStructuredGridData = _unStructuredGridData;



                //该   vtkPolyDataMapper   未使用  只是临时做测试用  
                _polyDataMapper = vtkPolyDataMapper.New();

                //_polyDataMapper.SetInput(_polyData);  
                //_polyDataMapper.ScalarVisibilityOn();
                //_polyDataMapper.ScalarVisibilityOff();
                //_polyDataMapper.SetLookupTable(lut);
                //_polyDataMapper.SetScalarRange(-1, 49);
                ////_polyDataMapper.SetScalarRange(_polyData.GetScalarRange()[0], _polyData.GetScalarRange()[1]);
                ////_polyDataMapper.SetColorModeToMapScalars();




                //cTop = vtkCellDataToPointData.New();
                //cTop.SetInput(_unStructuredGridData);


                #region 尝试添加颜色映射

                //创建查询表 

                //this.lut = this.ParentNode.Scene.LookUpTable.GetNewLookupTable();
                this.lut = null;

                if (lut == null)
                {

                    lut = vtkLookupTable.New();
                    //设置色调范围（红到蓝）
                    lut.SetHueRange(0.667, 1);
                    //设置饱和度范围
                    lut.SetSaturationRange(1, 1);
                    lut.SetValueRange(1, 1);
                    lut.SetNumberOfColors(256);

                    //lut.SetTableRange(0, 10);
                    lut.SetTableRange(0, 250);
                    //lut.SetNumberOfTableValues(5);
                    lut.SetNumberOfTableValues(6);


                    lut.Build();

                }



                #endregion


                setMapper = vtkDataSetMapper.New();


                setMapper.SetInput(_unStructuredGridData);
                //setMapper.SetInput(cTop .GetOutput ());


                setMapper.SetLookupTable(lut);
                setMapper.SetScalarRange(lut.GetTableRange()[0], lut.GetTableRange()[1]);


                setMapper.SetScalarModeToUsePointData();
                //setMapper.SetScalarModeToUseCellData ();



                //vtkDataSetMapper.SetResolveCoincidentTopologyToPolygonOffset();
                //vtkDataSetMapper.SetResolveCoincidentTopologyToShiftZBuffer();




                //if (this._contourType == CONTOURTYPE.Linear)
                //{
                    setMapper.ScalarVisibilityOff();
                //}
                //else
                //{
                //    setMapper.ScalarVisibilityOn();
                //}


                _actor = vtkActor.New();

                //_actor.SetMapper(_polyDataMapper);
                _actor.SetMapper(setMapper);

                _actor.GetProperty().SetColor(0, 0, 0);//黑色

                //_actor.SetScale(1,1,10);

                _actor.VisibilityOn();
                //_actor.VisibilityOff();



                _actor.GetProperty().LightingOff();//关闭自身的灯光 打开环境光 旋转时不会发生颜色的变化
                //_actor.GetProperty().LightingOn();

                #region

                //_actor.GetProperty().SetInterpolationToGouraud();
                //_actor.GetProperty().SetInterpolationToFlat ();
                //_actor.GetProperty().SetFrontfaceCulling(0);

                //_actor.GetProperty().BackfaceCullingOff();//cull剔除
                //_actor.GetProperty().BackfaceCullingOn();//角色背面被遮挡部分不进行渲染 和灯光无关
                //_actor.GetProperty().FrontfaceCullingOff();
                //_actor.GetProperty().FrontfaceCullingOn();//角色前面部分不进行渲染
              
                //_actor.GetProperty().SetOpacity(1);//透明度
                //_actor.GetProperty().SetOpacity(0);//透明度  完全透明  看不到了

                //_actor.GetProperty().ShadingOff();//Enable/Disable shading. When shading is enabled, the Material must be set. 
                //vectorActor.GetProperty().ShadingOn();

                //vtkDepthSortPolyData sort = vtkDepthSortPolyData.New();
                //sort .



                //_actor.GetProperty().SetAmbient(0);
                //_actor.GetProperty().SetSpecular(0);






                #endregion







                if (this._repType == REPRESENTATIONTYPE.POINT)
                {
                    _actor.GetProperty().SetRepresentationToPoints();
                }
                else if (this._repType == REPRESENTATIONTYPE.SURFACE)
                {
                    _actor.GetProperty().SetRepresentationToSurface();
                }
                else if (this._repType == REPRESENTATIONTYPE.WIREFRAME)
                {
                    _actor.GetProperty().SetRepresentationToWireframe();
                }


                this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().AddActor(_actor);


            }
            catch (Exception)
            {

                System.Windows.Forms.MessageBox.Show("ShowGeometryPolyDataFilter渲染过程出错！");
                return;
            }

        }

        public override void StopRender()
        {
            //资源的销毁

            #region VTK流水线的销毁

            if (this._actor != null)
            {
                this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().RemoveActor(_actor);
                this._actor.Dispose();
            }
            if (this._polyDataMapper != null)
            {
                this._polyDataMapper.Dispose();
            }

            if (this.setMapper !=null )
            {
                this.setMapper.Dispose();
            }

            if (this._polyData != null)
            {
                this._polyData.Dispose();
            }
            if (this._geo != null)
            {
                this._geo.Dispose();
            }
            if (this._con != null)
            {
                this._con.Dispose();
            }
            if (this._unStructuredGridData != null)
            {
                this._unStructuredGridData.Dispose();
            }
            //if (this.lut != null)
            //{
            //    this.lut.Dispose();
            //}

            if (this.ParentNode.Geometry.OutPutData!=null )
            {
                this.ParentNode.Geometry.OutPutData.Dispose();
            }

            //if (this.FilterTransform !=null )
            //{
            //    this.FilterTransform.Dispose();
            //}
        
            #endregion


            //throw new NotImplementedException();
        }


        #region 属性改变方法
       


        /// <summary>
        /// 重写可见性属性
        /// </summary>
        protected override void OnSetVisibility()
        {
            base.OnSetVisibility();

            if (this.ParentNode.Scene == null)
            {
                return;

            }

            if (this.ParentNode.Scene.IsRendering)
            {
                if (this._visiability)
                {
                    this._actor.VisibilityOn();
                }
                else
                {
                    this._actor.VisibilityOff();
                }
             
                this.ParentNode.Scene.Update();
            }
        }


        protected override void OnSetRepresentationType()
        {
            base.OnSetRepresentationType();


            if (this.ParentNode.Scene == null)
            {
                return;

            }

            if (this.ParentNode.Scene.IsRendering)
            {
                if (this._repType == REPRESENTATIONTYPE.POINT)
                {
                    this._actor.GetProperty().SetRepresentationToPoints();
                }
                else if (this._repType == REPRESENTATIONTYPE.SURFACE)
                {
                    this._actor.GetProperty().SetRepresentationToSurface();
                }
                else
                {
                    this._actor.GetProperty().SetRepresentationToWireframe();
                }


                this.ParentNode.Scene.Update();
            }

        }

        /// <summary>
        /// 重写着色显示类型
        /// </summary>
        protected override void OnSetContourType()
        {
            base.OnSetContourType();


            if (this.ParentNode.Scene == null)
            {
                return;

            }

            if (this.ParentNode.Scene.IsRendering)
            {
                if (this._contourType == CONTOURTYPE.Linear)
                {
                    this.setMapper.ScalarVisibilityOff();
                }
                else if (this._contourType == CONTOURTYPE.ColorFill)
                {
                    this.setMapper.ScalarVisibilityOn();
                }
                else if (this._contourType == CONTOURTYPE.LinearAndColorFill)
                {
                    this.setMapper.ScalarVisibilityOn();
                }


                this.ParentNode.Scene.Update();

            }

        }




        #endregion








    }

}
